# -*- coding: utf-8 -*-
"""
@Time ： 2023/4/7 21:36
@Auth ： 叶东宇
@File ：reverse_chain.py
"""
from Chain.Chain import *

"""
反转链表
"""
def reverse_chain(chain):

    # 无元素的情况下
    if chain.head.next is None:
        return Chain()
    # 有一个元素的情况
    elif chain.head.next.next is None:
        return chain
    # 有两个元素
    elif chain.head.next.next.next is None:
        chain.head.next = chain.head.next.next  # 最后一个结点变成第一个结点
        chain.head_point.next = None  # 第一个结点指向空
        chain.head_point = chain.head_point  # 变更尾结点
        chain.head.next.next = chain.head_point  # 原来第一个已经是最后一个节点了
        chain.head_point = chain.head.next  # 变更头结点
        return chain
    # 有超过三个元素的情况
    else:
        tmp_point_first = chain.head.next
        tmp_point_mid = chain.head.next.next
        tmp_point_last = chain.head.next.next.next
        tmp_point_first.next = None
        chain.tail_point = tmp_point_first
        while tmp_point_last is not None:
            tmp_point_mid.next = tmp_point_first
            tmp_point_first = tmp_point_mid
            tmp_point_mid = tmp_point_last
            tmp_point_last = tmp_point_last.next
        tmp_point_mid.next = tmp_point_first
        chain.head.next = tmp_point_mid
        chain.head_point = chain.head.next
        return chain


if __name__ == "__main__":
    lis = [1,2,3,4,5,6]
    chain = create_chain_head(lis)
    chain_ = reverse_chain(chain)
    print(chain.traverse())
